<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>客户管理</title>
<link rel="stylesheet" type="text/css" href="../static/jquery-easyui-1.3.3/themes/default/easyui.css"></link>
<link rel="stylesheet" type="text/css" href="../static/jquery-easyui-1.3.3/themes/icon.css"></link>
<script type="text/javascript" src="../static/jquery-easyui-1.3.3/jquery.min.js"></script>
<script type="text/javascript" src="../static/js/jqsetup.js"></script>
<script type="text/javascript" src="../static/js/jquery.form.js"></script>
<script type="text/javascript" src="../static/jquery-easyui-1.3.3/jquery.easyui.min.js"></script>
<script type="text/javascript" src="../static/jquery-easyui-1.3.3/locale/easyui-lang-zh_CN.js"></script>
<script type="text/javascript">

	var url;

	function formatEdit(val,row){
		return "<a href=\"javascript:openRoleChooseDialog('"+row.roles+"',"+row.id+")\"><img style='margin-top: 4px' src='../static/images/edit.gif' /></a>";
	}
	
	function openRoleChooseDialog(roles,customerId){
		var rolesArr=roles.split(",");
		$("#roleSetDialog").dialog("open").dialog("setTitle","选择角色");
		$("#roleDg").datagrid({
					url:'/admin/role/listAll',
					onLoadSuccess:function(data){
						var selectedRows=$("#roleDg").datagrid('getRows');
						for(var i=0;i<selectedRows.length;i++){
							var name=selectedRows[i].name;
							if($.inArray(name, rolesArr)>=0){
								$("#roleDg").datagrid('checkRow',i);
							}  
						}
					}
		});
		$("#customerId").val(customerId);
	}
	
	function saveRoleSet(){
		var customerId=$("#customerId").val();
		var selectedRows=$("#roleDg").datagrid("getSelections");
		var strRoleIds=[];
		for(var i=0;i<selectedRows.length;i++){
			strRoleIds.push(selectedRows[i].id);
		}
		var roleIds=strRoleIds.join(",");
		$.post("/admin/customer/saveRoleSet",{roleIds:roleIds,customerId:customerId},function(result){
			if(result.success){
				closeRoleSetDialog();
				$("#dg").datagrid("reload");
			}else{
				$.messager.alert("系统提示","提交失败，请联系管理员！");
			}
		},"json");
	}
	
	function closeRoleSetDialog(){
		 $("#roleSetDialog").dialog("close");
	 }
	
	
	
	
	function deleteCustomer(){
		var selectedRows=$("#dg").datagrid("getSelections");
		if(selectedRows.length==0){
			 $.messager.alert("系统提示","请选择要删除的数据！");
			 return;
		 }
		var strIds=[];
		 for(var i=0;i<selectedRows.length;i++){
			 strIds.push(selectedRows[i].id);
		 }
		 var ids=strIds.join(",");
		 $.messager.confirm("系统提示","您确定要删除这<font color=red>"+selectedRows.length+"</font>条数据吗？",function(r){
				if(r){
					$.post("/admin/customer/delete",{ids:ids},function(result){
						if(result.success){
							 $.messager.alert("系统提示","数据已成功删除！");
							 $("#dg").datagrid("reload");
						}else{
							$.messager.alert("系统提示",result.errorInfo);
						}
					},"json");
				} 
	   });
	}
	
	
	function openCustomerAddDialog(){
		$("#dlg").dialog("open").dialog("setTitle","添加客户信息");
		url="admin/customer/save";
	}
	
	function openCustomerModifyDialog(){
		var selectedRows=$("#dg").datagrid("getSelections");
		 if(selectedRows.length!=1){
			 $.messager.alert("系统提示","请选择一条要修改的数据！");
			 return;
		 }
		 var row=selectedRows[0];
		 $("#dlg").dialog("open").dialog("setTitle","修改客户信息");
		 $("#fm").form("load",row);
		 $("#customerName").attr("readonly","readonly")
		 url="admin/customer/save?id="+row.id;
	 }
	
	
	
	function saveCustomer(){
		var options = {
			url: url, //提交地址：默认是form的action,如果申明,则会覆盖
			beforeSubmit: function(){
				return $("#fm").form("validate");
			}, //提交前的回调函数
			success: function(result){
				//var result=eval('('+result+')');
				if(result.success){
					$.messager.alert("系统提示","保存成功！");
					resetValue();
					$("#dlg").dialog("close");
					$("#dg").datagrid("reload");
				}else{
					$.messager.alert("系统提示",result.errorInfo);
				}
			}, //提交成功后的回调函数
			dataType: "json", //html(默认), xml, script, json...接受服务端返回的类型
			clearForm: true, //成功提交后，是否清除所有表单元素的值
			resetForm: true, //成功提交后，是否重置所有表单元素的值
			timeout: 3000 //限制请求的时间，当请求大于3秒后，跳出请求
		};
		$("#fm").ajaxSubmit(option);
	 }
	
	
	
	function resetValue(){
		 $("#name").val("");
		 $("#contact").val("");
		 $("#number").val("");
		 $("#address").val("");
		 $("#remarks").val("");
	 }
	
	 function closeCustomerDialog(){
		 $("#dlg").dialog("close");
		 resetValue();
	 }
	 
	 function searchCustomer(){
		$("#dg").datagrid('load',{
			"name":$("#s_name").val() 
		});	
	}
	 
	$(function () {  
		 $("[name=token]").val(window.localStorage.getItem('access-token'));
		 $("#dg").datagrid({  
		        //双击事件  
		        onDblClickRow: function (index, row) {  
		        	$("#dlg").dialog("open").dialog("setTitle","修改客户信息");
			   		 $("#fm").form("load",row);
			   		 $("#customerName").attr("readonly","readonly")
			   		 url="admin/customer/save?id="+row.id;
		        }  
		    });  
		 
		 
	 });
	 

</script>
</head>
<body style="margin: 1px">
<table id="dg" title="客户管理" class="easyui-datagrid"
   fitColumns="true" pagination="true" rownumbers="true" 
   url="/admin/customer/list" fit="true" toolbar="#tb">
   <thead>
   	<tr>    
   	    <th field="cb" checkbox="true" align="center"></th>
   		<th field="id" width="20" align="center">编号</th>
   		<th field="name" width="100" align="center">客户名称</th>
   		<th field="contact" width="50" align="center">联系人</th>
   		<th field="number" width="50" align="center">联系电话</th>
   		<th field="address" width="150" align="center">联系地址</th>
   		<th field="remarks" width="150" align="center">备注</th>
   	</tr>
   </thead>
 </table>
 
 <div id="tb">
 	<div>
 		<a href="javascript:openCustomerAddDialog()" class="easyui-linkbutton" iconCls="icon-add" plain="true">添加</a>
 		<a href="javascript:openCustomerModifyDialog()" class="easyui-linkbutton" iconCls="icon-edit" plain="true">修改</a>
 		<a href="javascript:deleteCustomer()" class="easyui-linkbutton" iconCls="icon-remove" plain="true">删除</a>
 	</div> 
 	<div>
 		&nbsp;客户名称：&nbsp;<input type="text" id="s_name" size="20" onkeydown="if(event.keyCode==13) searchCustomer()"/>
 		<a href="javascript:searchCustomer()" class="easyui-linkbutton" iconCls="icon-search" plain="true">搜索</a>
 	</div>                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                  
 </div>
 
 <div id="dlg" class="easyui-dialog" style="width:500px;height:350px;padding: 10px 20px"
   closed="true" buttons="#dlg-buttons" data-options="onClose:function(){resetValue()}">
   
   <form id="fm" method="post">
		 <input type="hidden"  id="token" name="token" value="" />
   	<table cellspacing="8px">
   		<tr>
   			<td>客户名称：</td>
   			<td><input type="text" id="name" name="name" class="easyui-validatebox" required="true" style="width: 250px"/></td>
   		</tr>
   		<tr>
   			<td>联系人：</td>
   			<td><input type="text" id="contact" name="contact" class="easyui-validatebox"  required="true" style="width: 250px"/></td>
   		</tr>
   		<tr>
   			<td>联系电话：</td>
   			<td><input type="text" id="number" name="number" class="easyui-validatebox"  required="true" style="width: 250px"/></td>
   		</tr>
   		<tr>
   			<td>联系地址：</td>
   			<td><input type="text" id="address" name="address" class="easyui-validatebox"  required="true" style="width: 250px"/></td>
   		</tr>
   		<tr>
   			<td valign="top">备注：</td>
   			<td>
   				<textarea rows="5" cols="40" id="remarks" name="remarks"></textarea>
   			</td>
   		</tr>
   	</table>
   </form>
 </div>
 
 <div id="dlg-buttons">
 	<a href="javascript:saveCustomer()" class="easyui-linkbutton" iconCls="icon-ok">保存</a>
 	<a href="javascript:closeCustomerDialog()" class="easyui-linkbutton" iconCls="icon-cancel">关闭</a>
 </div>
 
  
 
</body>
</html>